﻿using System;
using System.IO;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Data.SqlClient;
using VPLink2013.Framework;

namespace VPLink2013.Webcontrols
{
    public class MoteurRecherche: WebcontrolVPLink
    {
		protected string ReadUtilisateurs()
		{
			StringBuilder sbUtilisateurs = new StringBuilder();
			sbUtilisateurs.AppendLine( "var utilisateurs=[" );
			string strSql = "SELECT DISTINCT UserName FROM Users WHERE UserName IS NOT NULL and Actif = 1";

			SPSecurity.RunWithElevatedPrivileges( delegate()
			{
                if (!string.IsNullOrEmpty(connectionString))
                {
                    SqlConnection conn = VPLink2013SqlHelper.GetConnection(connectionString);
                    if (conn != null)
                    {
                        SqlCommand command = VPLink2013SqlHelper.GetCommand(conn, strSql);
                        if (command != null)
                        {
                            SqlDataReader Reader = command.ExecuteReader();
                            Boolean blnFirst = true;
                            while (Reader.Read())
                            {
                                string strNom = VPLink2013SqlHelper.GetValueReader(Reader, 0);

                                if (!blnFirst)
                                {
                                    sbUtilisateurs.AppendLine(",");
                                }
                                sbUtilisateurs.AppendLine("{ Nom:\"" + strNom + "\"}");
                                blnFirst = false;
                            }
                            sbUtilisateurs.AppendLine("]");
                            VPLink2013SqlHelper.CloseReader(conn, Reader);
                        }
                    }
                }
			} );
			return sbUtilisateurs.ToString();
		}

		protected override void Render( HtmlTextWriter writer )
		{
            string strHtml = string.Empty;
            if (rechercheVersion == "VPLink2014")
			{
				Template = @"webcontrol MoteurRecherche2014";
                string strHtmlFileCache = Path.Combine(pathCache, Template + ".html");

                if (FileSerialization.CheckFileCacheExists(strHtmlFileCache, 180))
				{
					strHtml = System.IO.File.ReadAllText( strHtmlFileCache );
					ContextTrace.AddTrace( "File cache found !" );
				}
				else
				{
					string strHtmlFileCacheInitial = pathCache + @"\Annuaire\Annuaire autocomplete2014.htm";
                    if (FileSerialization.CheckFileCacheExists(strHtmlFileCacheInitial))
                    {
                        try
                        {
                            string strHtmlInitial = System.IO.File.ReadAllText(strHtmlFileCacheInitial);
                            string strUtilisateurs = ReadUtilisateurs();
                            strHtml = strHtmlInitial.Replace("*Utilisateurs*", strUtilisateurs);
                            FileSerialization.TraceFile(strHtmlFileCache, strHtml);
                        }
                        catch (Exception ex)
                        {
                            ContextTrace.AddTrace(string.Format("Exception lors de recherche bd des utilisateurs.", ex.Message));
                            ContextTrace.AddTrace(ex.StackTrace);
                        }
                    }
				}
			}
			else
			{
				Template = "webcontrol MoteurRecherche";
			}
            sbHtml.Append(strHtml);
			base.Render( writer );
		}
    }
}
